import $service from '../../../../../service.js';
var doctor_id; //医生id
var hospital_id = "1";
var log_id;
var that;
Page({
    data: { //遍历的数据
        active: true,
        hiddenName: true, //下拉框内容
        selected: 1,
        hiddenpop: true, //弹窗
        showModal: false,
        inquiryMap:{},
        guidance_content:'',
        doctorMap:{},
        guidanceList:[],
        isdetalis:false,
        guidance_nameText:''
    },
    onLoad(e) { //拿到上一页传过来的参数
        log_id = e.log_id;
        var map = $service.strToJson(e.Map)
        this.setData({
            guidance_content: map.guidance_content
        })
    },
    onShow() { //加载页面
        that =this.data;
        doctor_id = $service.getUserLocalStorage().doctor_id;
        if (!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)) {
            hospital_id = $service.getUserLocalStorage().hospital_id
        }
        this.publicUtil_getInquiryLogDetails()
    },
    publicUtil_getInquiryLogDetails: function() {
        var jsonData = {
            "is_receive": "1",
            "log_id": log_id
        }

        var jsonAjax = {
            "url": "publicUtil_getInquiryLogDetails.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.publicUtil_getInquiryLogDetails_back);
    },
    publicUtil_getInquiryLogDetails_back: function(jsonBack) {
       var data=$service.strToJson(jsonBack.data).data;
       var prescriptionList = data.prescriptionList; 
       var inquiryMap= data.inquiryMap; //患者信息
       var doctorMap = data.doctorMap; //医生信息
       inquiryMap.lrsj2 = $service.nothinglrsj(inquiryMap.lrsj)
       
       if (!$service.isUndefinedAndEmpty(inquiryMap.lof_diagnosis)) {
           var lof_diagnosis = '';
           var lof_diagnosis2 = '';
           var numC =0
           var lofdiagnosislist = inquiryMap.lof_diagnosis.split(",")
           for (var i = 0; i < lofdiagnosislist.length; i++) {
               var index = lofdiagnosislist[i].indexOf('自定义诊断');
               if (index > -1) {
                   lof_diagnosis2 += lofdiagnosislist[i] + ",";
               } else {
                   numC++
                   lof_diagnosis += "(" + numC + ")" + lofdiagnosislist[i] + ",";
               }
           }
           inquiryMap.diagnosisMap = lof_diagnosis + lof_diagnosis2;
           inquiryMap.diagnosisMap = inquiryMap.diagnosisMap.substring(0, inquiryMap.diagnosisMap.length - 1);
       } else {
           inquiryMap.diagnosisMap =''
       }
       this.setData({
           inquiryMap : inquiryMap,
           doctorMap: doctorMap,
           guidanceList:data.guidanceList
       })
    },
    addHealthGuidance: function() {
        if($service.isUndefinedAndEmpty(that.inquiryMap.log_main_suit)){
            $service.showToast("请先输入主诉")
            return;
        }
        if($service.isUndefinedAndEmpty(that.inquiryMap.diagnosisMap) ){
            $service.showToast("请先输入诊断")
            return;
        }
        if($service.isUndefinedAndEmpty(that.guidance_content)){
            $service.showToast('请输入指导内容');
            return;
        }
        if(that.guidanceList.length > 5){
            $service.showToast('意见指导最多5条');
            return;
        }
        var jsonData = {
            "doctor_id": doctor_id,
            "log_id": that.inquiryMap.log_id,
            "hospital_id": hospital_id,
            "user_type": that.inquiryMap.join_type,
            "user_id": that.inquiryMap.join_id,
            "inquiry_patient_id": that.inquiryMap.inquiry_patient_id,
            "guidance_content": that.guidance_content
        }

        var jsonAjax = {
            "url": "doctorSide_personal_inquiry_addHealthGuidance.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_inquiry_addHealthGuidance_back);
    },
    doctorSide_personal_inquiry_addHealthGuidance_back: function(jsonBack) {
        var data = $service.strToJson(jsonBack.data).data;
        var selToID='';
        if(that.inquiryMap.join_type == 1){
             selToID = "U" + that.inquiryMap.join_id
        }else{
              selToID = "S" + that.inquiryMap.join_id
        }
        var sendSystemMessageMsgText = '医生已开具健康指导意见'
        wx.txTic.sendCustomMessage(selToID,6)
        wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
        $service.showToast('指导内容生成成功');
        this.setData({
            guidance_content : '',
            selected: 2
        })
        this.publicUtil_getInquiryLogDetails()
    },
    drop_down(e) { //
        console.log(e)
        this.setData({
            hiddenName: !this.data.hiddenName
        })
    },
    pop: function() { //弹窗
        this.setData({
            showModal: true
        })
    },
    // 禁止屏幕滚动
    ms: function() {},
    // 弹出层里面的弹窗
    ok: function() {
        this.setData({
            showModal: false
        })
    },
    selected: function(e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            selected: index
        })
    },
    gotodetalis(e){
        var index = e.currentTarget.dataset.index;
        if(index == 0 || index == 1 || index == 2 || index == 3 ||index == 4 ){//0过敏史 1 主诉 2现病史 3既往史 4诊断
            $service.openPage('../../index_diagnosis/index_diagnosis?inquiryMap=' + $service.jsonToStr(that.inquiryMap) +'&index='+ index)
        }
    },
    inputChange(e) { //输入框监听
        var index = e.currentTarget.dataset.index;
        if(index == 1){
            that.guidance_content = e.detail.value
        }
        this.setData({
            guidance_content: that.guidance_content
        })
    },
    gotopage(e) {//1删除模板 2查看模板 3引用意见单 4关闭弹窗
        var this_= this
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        if(index == 1){
            wx.showModal({
                title: '温馨提示',
                content: '确定要作废此意见单？',
                success(res) {
                    if (res.confirm) {
                        var jsonData ={
                            "guidance_id" : this_.data.guidanceList[num].guidance_id,
                        }
                        var jsonAjax = {
                            "url": "doctorSide_personal_inquiry_deleteLogHealthGuidance.do",
                            "jsonData": jsonData,
                        }
                        $service.getAjaxData(jsonAjax, function(jsonObj) {
                            var selToID='';
                            if(that.inquiryMap.join_type == 1){
                                 selToID = "U" + that.inquiryMap.join_id
                            }else{
                                  selToID = "S" + that.inquiryMap.join_id
                            }
                            wx.txTic.sendCustomMessage(selToID,6)
                            var sendSystemMessageMsgText = '医生已撤销健康指导意见'
                            wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
                            $service.showToast('作废成功')
                            this_.publicUtil_getInquiryLogDetails()
                        });
                    }
                }
            }) 
        }else if(index == 2){
            this.setData({
                isdetalis:true,
                guidance_nameText:this_.data.guidanceList[num].guidance_content
            })
        }else if(index == 3){
            $service.openPage('../../index_template/index_template?type=1&log_id=' + log_id +'&guidance_content=' + this_.data.guidanceList[num].guidance_content)
        }else if(index == 4){
           this.setData({
               isdetalis:false
           })
        }else if(index == 5){
            $service.openPage('../../index_template/index_template?type=1&log_id=' + log_id +'&guidance_content=' + that.guidance_content)
        }
        
    },
})
